<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="82px">
      <el-form-item label="店铺cn码" prop="storeUserName">
        <el-input
          v-model="queryParams.storeUserName"
          placeholder="请输入店铺cn码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['seller:sellerStoreDiagnosisRecord:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sellerStoreDiagnosisRecordList" border
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="密码" align="center" prop="storePassWord"/>
      <el-table-column label="外部店铺" align="center" prop="userName"/>
      <el-table-column label="cn码" align="center" prop="storeUserName"/>
      <el-table-column label="概要" align="center" prop="summary"/>
      <el-table-column label="详情" align="center" prop="details">
        <template slot-scope="scope">
          <div v-if="scope.row.details != '待诊断' ">
            <el-link type="primary" :href="$store.getters.domain + scope.row.details" target="_blank">
              下载文档
            </el-link>
          </div>
          <div v-else>
            {{ scope.row.details }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="最近诊断时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)">修改-->
          <!--          </el-button>-->
          <el-button size="mini" type="text" @click="openUploadDiagnosisModal(scope.row)">上传</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改诊断记录对话框 -->
    <el-dialog :title="title" :visible.sync="isOpenDiagnosisModal" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺cn码" prop="storeUserName">
          <el-input v-model="form.storeUserName" disabled placeholder="请输入店铺cn码"/>
        </el-form-item>
        <el-form-item label="概要" prop="summary">
          <el-input v-model="form.summary" placeholder="请输入概要"/>
        </el-form-item>
        <el-form-item label="详情" prop="details">
          <!--          <el-input v-model="form.details" placeholder="请输入详情"/>-->
          <file-upload
            :fileType="['doc']"
            v-model="form.details"
            :limit="1"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addStoreDiagnosisRecord, getStoreDiagnosisRecord} from "@/api/diagnosis/index";

export default {
  name: "SellerStoreDiagnosisRecord",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 诊断记录表格数据
      sellerStoreDiagnosisRecordList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeUserName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storeUserName: [{required: true, message: "cn码不能为空", trigger: "blur"}],
        summary: [{required: true, message: "概要不能为空", trigger: "blur"}],
        details: [{required: true, message: "详情不能为空", trigger: "blur"}],
      },
      isOpenDiagnosisModal: false,//是否打开上传诊断记录弹窗
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询诊断记录列表 */
    getList() {
      this.loading = true;
      getStoreDiagnosisRecord(this.queryParams).then(response => {
        this.sellerStoreDiagnosisRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.isOpenDiagnosisModal = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        // storeUserName: undefined,
        summary: undefined,
        details: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 打开上传诊断记录弹窗
    openUploadDiagnosisModal(row) {
      this.reset();
      this.form.storeUserName = row.storeUserName
      this.isOpenDiagnosisModal = true
      this.title = "上传诊断信息";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$confirm('是否确定上传诊断信息？', '提示').then(() => {
            this.buttonLoading = true;
            // console.log('this.form', this.form)
            addStoreDiagnosisRecord(this.form).then(response => {
              this.$message.success(response.msg);
              this.isOpenDiagnosisModal = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          })
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('seller/sellerStoreDiagnosisRecord/export', {
        ...this.queryParams
      }, `sellerStoreDiagnosisRecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
